package com.info.zhiduoduo.repository.mapper.crowdsource;

import com.baomidou.mybatisplus.mapper.BaseMapper;
import com.info.zhiduoduo.repository.dto.crowdsource.CrowdsourceProjectMatchDto;
import com.info.zhiduoduo.repository.entity.crowdsource.CrowdsourceProjectMatch;

import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.Date;
import java.util.List;

/**
 * <p>
 *  Mapper 接口
 * </p>
 *
 * @author bob
 * @since 2020-11-15
 */
public interface CrowdsourceProjectMatchMapper extends BaseMapper<CrowdsourceProjectMatch> {

	@Select(" select cpm.* " +
			" from zdd_crowdsource_project_match cpm " +
			" where 1=1 " +
			" and if(#{compid}=0, 1=1, cpm.compid=#{compid}) " +
			" and if(#{excludeCompid}=0, 1=1, cpm.compid!=#{excludeCompid}) " +
			" and cpm.state = 1 " +
			" order by cpm.id ")
	List<CrowdsourceProjectMatch> listUnmatchedByCompid(@Param("compid") int compid, @Param("excludeCompid") int excludeCompid);

	@Select(" select cpm.* " +
			" from zdd_crowdsource_project_match cpm " +
			" where projectid = #{projectid} ")
	CrowdsourceProjectMatch getByProjectid(Integer projectid);

	@Select(" select cpm.compid,c.compname,cpm.projectid,cp.project_name,cp.project_total_amount,cp.project_total_service_charge,cp.people_count," +
			" cpm.match_compid,mc.compname as match_compname,cpm.match_projectid,mcp.project_name as match_project_name," +
			" mcp.project_total_amount as match_project_total_amount,mcp.project_total_service_charge as match_project_total_service_charge," +
			" mcp.people_count as match_people_count," +
			" cp.state as projectState,cpm.state,cpm.createtime,cpm.match_time " +
			" from zdd_crowdsource_project_match cpm " +
			" join zdd_company c on c.compid = cpm.compid " +
			" join zdd_crowdsource_project cp on cp.projectid = cpm.projectid " +
			" left join zdd_company mc on mc.compid = cpm.match_compid " +
			" left join zdd_crowdsource_project mcp on mcp.projectid = cpm.match_projectid " +
			" where cpm.compid = #{compid} " +
			" and CONCAT(cp.project_name,IFNULL(mc.compname,''),IFNULL(mcp.project_name,'')) like '%${name}%' " +
			" and IF(#{state}=0,1=1,cpm.state=#{state}) " +
			" and IF(ISNULL(#{startDate}),1=1, cpm.createtime >= #{startDate}) "  +
			" and IF(ISNULL(#{endDate}),1=1, cpm.createtime <= #{endDate}) " +
			" and cp.deleted = 0 ")
	List<CrowdsourceProjectMatchDto> listprojectMatch(@Param("compid") int compid, @Param("name") String name, @Param("state") int state, @Param("startDate") Date startDate,
	                                                  @Param("endDate") Date endDate);
}
